package com.tjsinfo.tjsoa.addresslist.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListGroupBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListSystemUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListGroup;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListSystemUser;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListUser;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class ReturnSynchronizationAction extends Action{
	private IAddressListUserBo listUserBo;
	private IAddressListGroupBo groupBo;
	private ISystemUserBo tjsUserBo;
	private IAddressListAndFilePendingBo addressListAndFilePendingBo;
	private IAddressListSystemUserBo addressListSystemUserBo;
	/*private ITjsMessageBo tjsMessageBo;

	public void setTjsMessageBo(ITjsMessageBo tjsMessageBo) {
		this.tjsMessageBo = tjsMessageBo;
	}*/

	public void setAddressListSystemUserBo(
			IAddressListSystemUserBo addressListSystemUserBo) {
		this.addressListSystemUserBo = addressListSystemUserBo;
	}

	public void setAddressListAndFilePendingBo(
			IAddressListAndFilePendingBo addressListAndFilePendingBo) {
		this.addressListAndFilePendingBo = addressListAndFilePendingBo;
	}

	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}

	public void setListUserBo(IAddressListUserBo listUserBo) {
		this.listUserBo = listUserBo;
	}

	public void setGroupBo(IAddressListGroupBo groupBo) {
		this.groupBo = groupBo;
	}
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	private Date date = null;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String returnType = request.getParameter("returnType");
		String userid = request.getParameter("userid");
		String messageid = request.getParameter("messageid");
		
		addressListAndFilePendingBo.deleteById(messageid);
		
		if (returnType.equals("yes")) {
			String tjsUserId = request.getParameter("tjsUserId");
			AddressListSystemUser tjsUser = addressListSystemUserBo.findByUserId(Integer.valueOf(tjsUserId));
			TSystemUser tjsuser = tjsUserBo.findByIdSystemUser(Integer.valueOf(userid));
			
			List list = listUserBo.getAddressListUserTongBu(tjsuser,Integer.valueOf(tjsUserId));
			
			String groupid = request.getParameter("groupid");
			
			String userName = tjsUser.getUserName();
			String mobile = tjsUser.getMobile();
			String email = tjsUser.getEmail();
			
			String birthday = null;
			
			String homeAddress = tjsUser.getHomeAddress();
			String homePhone = tjsUser.getHomePhone();
			String homePostal = tjsUser.getHomePostal();
			
			String company = tjsUser.getCompany();
			String sector = tjsUser.getSector();
			String duty = tjsUser.getDuty();
			String companyPhone = tjsUser.getCompanyPhone();
			String companyFax = tjsUser.getCompanyFax();
			String companyAddress = tjsUser.getCompanyAddress();
			String companyPostal = tjsUser.getCompanyPostal();
			
			String qq = tjsUser.getQq();
			String msn = tjsUser.getMsn();
			String blog = tjsUser.getBlog();
			String remarks = tjsUser.getRemarks();
			
			AddressListGroup group = new AddressListGroup();
			if (groupid != null && !"".equals(groupid)) {
				group = groupBo.findById(groupid);
			}
			
			
			AddressListUser listUser = new AddressListUser();
			listUser.setAddressListGroup(group);
			listUser.setTjsUser(tjsuser);
			listUser.setUserName(userName);
			listUser.setMobile(mobile);
			listUser.setEmail(email);
			listUser.setHomeAddress(homeAddress);
			listUser.setHomePhone(homePhone);
			listUser.setHomePostal(homePostal);
			listUser.setCompany(company);
			listUser.setSector(sector);
			listUser.setDuty(duty);
			listUser.setCompanyPhone(companyPhone);
			listUser.setCompanyFax(companyFax);
			listUser.setCompanyAddress(companyAddress);
			listUser.setCompanyPostal(companyPostal);
			if (tjsUser.getBirthday() != null) {
				listUser.setBirthday(tjsUser.getBirthday());
				Date date = tjsUser.getBirthday();
				birthday = date.toString().split(" ")[0];
			}
			listUser.setQq(qq);
			listUser.setMsn(msn);
			listUser.setBlog(blog);
			listUser.setState(Byte.valueOf("1"));
			if (remarks != null && !"".equals(remarks)) {
				listUser.setRemarks(remarks.trim());
			}else{
				remarks = "";
			}
			listUser.setUserId(Integer.valueOf(tjsUserId));
			
			if (list.size() > 0) {
				listUserBo.updatesTongBu(tjsUserId, userName, mobile, email, company, sector, duty, companyPhone, companyFax, companyAddress, companyPostal, homeAddress, homePostal, homePhone, birthday, qq, msn, blog, remarks);
			}else {
				listUserBo.saveAddressListUser(listUser);
			}
		}
		
		request.setAttribute("first", "shi");
		request.setAttribute("moduleType", "1");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return mapping.findForward("ok");
		}else {
			return mapping.findForward("sessionok");
		}
	}
}
